/*
 * D.A.O. for JPA  
 * Created on 6 juin 2013 ( Time 15:03:32 )
 */

package org.demo.employe.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.telosys.starterkits.strutsjpa.CoreUtils;
import org.telosys.starterkits.strutsjpa.GenericJpaDao;

import org.demo.employe.bean.Employe;

public class JpaEmployeDAO extends GenericJpaDao<Employe, Integer> {

	private static final Predicate[] ap = {};
	
	/**
	 * Constructs a DAO for Employe
	 */
	public JpaEmployeDAO() {
		super(Employe.class);
	}
	
	/**
	 * Search by Employe example
	 */
	public List<Employe> search(final Employe element) {
		final EntityManager em = getEntityManager();
		if ( element != null  ) {
			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Employe> criteriaQuery = criteriaBuilder.createQuery(Employe.class);
			Root<Employe> from = criteriaQuery.from(Employe.class);

			List<Predicate> listP = new ArrayList<Predicate>();


			if (CoreUtils.isNotBlank(element.getIdemploye())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idemploye"), element.getIdemploye());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdpointage())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idpointage"), element.getIdpointage());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdgroupe())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idgroupe"), element.getIdgroupe());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdrole())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idrole"), element.getIdrole());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getMatriculeemploye())) {
				Predicate predicate = criteriaBuilder.equal(from.get("matriculeemploye"), element.getMatriculeemploye());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getNomemploye())) {
				Predicate predicate = criteriaBuilder.equal(from.get("nomemploye"), element.getNomemploye());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getPrenomemploye())) {
				Predicate predicate = criteriaBuilder.equal(from.get("prenomemploye"), element.getPrenomemploye());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getCin())) {
				Predicate predicate = criteriaBuilder.equal(from.get("cin"), element.getCin());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getMailemploye())) {
				Predicate predicate = criteriaBuilder.equal(from.get("mailemploye"), element.getMailemploye());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getFonction())) {
				Predicate predicate = criteriaBuilder.equal(from.get("fonction"), element.getFonction());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getLogin())) {
				Predicate predicate = criteriaBuilder.equal(from.get("login"), element.getLogin());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getPassword())) {
				Predicate predicate = criteriaBuilder.equal(from.get("password"), element.getPassword());
				listP.add(predicate);
			}
			
			criteriaQuery.where(listP.toArray(ap));
			TypedQuery<Employe> tq = em.createQuery(criteriaQuery);
			return tq.getResultList();
		}
		else
		{
    		return this.loadAll();
		}
	}

}